<?php
/**
 * Created by PhpStorm.
 * User: tao
 * Date: 12/4/2017
 * Time: 1:16 PM
 */

use Overtrue\Pinyin\Pinyin;

class ChineseController extends  Controller
{
    /**
     * Declares class-based actions.
     */
    public function actions()
    {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha'=>array(
                'class'=>'CCaptchaAction',
                'backColor'=>0xFFFFFF,
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page'=>array(
                'class'=>'CViewAction',
            ),
        );
    }
    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            //'postOnly + delete', // we only allow deletion via POST request
        );
    }
    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array(
                'allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions'=>array(
                    'index',
                ),
                'users'=>array('*'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

    public function actionIndex() {
        $this->layout = 'blank';

        // 小内存型
        $pinyin = new Pinyin(); // 默认
        // 内存型
        // $pinyin = new Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
        // I/O型
        // $pinyin = new Pinyin('Overtrue\Pinyin\GeneratorFileDictLoader');

        $a = $pinyin->convert('带着希望去旅行，比到达终点更美好');
        // ["dai", "zhe", "xi", "wang", "qu", "lv", "xing", "bi", "dao", "da", "zhong", "dian", "geng", "mei", "hao"]
        echo '<br/>';
        var_dump($a);

        $a = $pinyin->convert('带着希望去旅行，比到达终点更美好', PINYIN_UNICODE);
        // ["dài","zhe","xī","wàng","qù","lǚ","xíng","bǐ","dào","dá","zhōng","diǎn","gèng","měi","hǎo"]
        echo '<br/>';
        var_dump($a);

        $a = $pinyin->convert('带着希望去旅行，比到达终点更美好', PINYIN_ASCII);
        //["dai4","zhe","xi1","wang4","qu4","lv3","xing2","bi3","dao4","da2","zhong1","dian3","geng4","mei3","hao3"]
        echo '<br/>';
        var_dump($a);

        //生成用于链接的拼音字符串
        $a1 = $pinyin->permalink('带着希望去旅行'); // dai-zhe-xi-wang-qu-lv-xing
        $a2 = $pinyin->permalink('带着希望去旅行', '.'); // dai.zhe.xi.wang.qu.lv.xing
        echo '<br/>';
        echo $a1;
        echo '<br/>';
        echo $a2;

        //获取首字符字符串
        $a1 = $pinyin->abbr('带着希望去旅行'); // dzxwqlx
        $a2 = $pinyin->abbr('带着希望去旅行', '-'); // d-z-x-w-q-l-x
        echo '<br/>';
        echo $a1;
        echo '<br/>';
        echo $a2;

        //翻译整段文字为拼音
        //将会保留中文字符：，。 ！ ？ ： “ ” ‘ ’ 并替换为对应的英文符号。
        $a1 = $pinyin->sentence('带着希望去旅行，比到达终点更美好！');
        // dai zhe xi wang qu lv xing, bi dao da zhong dian geng mei hao!
        $a2 = $pinyin->sentence('带着希望去旅行，比到达终点更美好！', true);
        // dài zhe xī wàng qù lǚ xíng, bǐ dào dá zhōng diǎn gèng měi hǎo!
        echo '<br/>';
        echo $a1;
        echo '<br/>';
        echo $a2;

        //翻译姓名
        //姓名的姓的读音有些与普通字不一样，比如 ‘单’ 常见的音为 dan，而作为姓的时候读 shan。
        $a1 = $pinyin->name('单某某'); // ['shan', 'mou', 'mou']
        $a2 = $pinyin->name('单某某', PINYIN_UNICODE); // ["shàn","mǒu","mǒu"]
        echo '<br/>';
        var_dump($a1);
        echo '<br/>';
        var_dump($a2);

        //六安
        echo '<br/>';
        $a = $pinyin->name('六安');
        var_dump($a);

        //$this->render('index');
    }
}